#!/bin/bash
[% c("var/set_default_env") -%]
export PROJECT_PATH=/var/tmp/dist/[% project %]
export SDK_HOME=$PROJECT_PATH/android-sdk-linux
export NDK_HOME=$PROJECT_PATH/android-ndk
export GRADLE_HOME=$PROJECT_PATH/gradle
android_release_dir=android-10
android_release_dir_old=android-9

# Make directories
mkdir -p $NDK_HOME $SDK_HOME $GRADLE_HOME

# Compiler Archives
unzip -qq [% c("input_files_by_name/android_sdk_tools") %] -d $SDK_HOME
unzip -qq [% c("input_files_by_name/android_ndk_compiler")%] -d $NDK_HOME
# GeckoView is still using an older Android NDK, bundle it, too.
unzip -qq [% c("input_files_by_name/android_ndk_compiler_old")%] -d $NDK_HOME
unzip -qq [% c("input_files_by_name/gradle_bin") %] -d $GRADLE_HOME
# GeckoView is still using an older Gradle, bundle it, too.
unzip -qq [% c("input_files_by_name/gradle_bin_old") %] -d $GRADLE_HOME
# application-services is using a comparatively new Gradle, bundle it, too.
unzip -qq [% c("input_files_by_name/gradle_bin_new") %] -d $GRADLE_HOME

# NDK Archive
for version in [% c("var/android_ndk_version") %][% c("var/android_ndk_revision") %] [% c("var/android_ndk_version_old") %]; do
  cd $NDK_HOME
  # Adjust cross compilation prefix for armv7a. Unfortunately, the compiler cross
  # compilation prefix and the one for all the other tools differ. Let's fix that
  # so we only have to take care of one. See: #33557 (comment 2674649).
  cd android-ndk-r$version/toolchains/llvm/prebuilt/linux-x86_64/bin
  for f in `ls arm-linux-androideabi-*`; do
    ln -s $f ${f//arm-linux-androideabi/armv7a-linux-androideabi}
  done
done

# Tool Archives
cd $SDK_HOME
unzip -qq $HOME/[% c("input_files_by_name/build_tools") %] -d $SDK_HOME
mkdir -p build-tools/[% c("version") %]
mv $android_release_dir/* build-tools/[% c("version") %]

# Fenix has not updated to build-tools 29 yet, bundle therefore build-tools 28,
# too.
unzip -qq $HOME/[% c("input_files_by_name/build_tools_old") %] -d $SDK_HOME
mkdir -p build-tools/[% c("version_old") %]
mv $android_release_dir_old/* build-tools/[% c("version_old") %]

unzip -qq $HOME/[% c("input_files_by_name/platform") %] -d $SDK_HOME
mkdir -p platforms/android-[% c("var/android_api_level") %]
mv $android_release_dir/* platforms/android-[% c("var/android_api_level") %]

# Fenix has not updated to Android 29 yet, bundle therefore Android 28, too.
unzip -qq $HOME/[% c("input_files_by_name/platform_old") %] -d $SDK_HOME
mkdir -p platforms/android-[% c("var/android_api_level_old") %]
mv $android_release_dir_old/* platforms/android-[% c("var/android_api_level_old") %]

unzip -qq $HOME/[% c("input_files_by_name/platform_tools") %] -d $SDK_HOME

# That's a quirk required by GeckoView but let's have it here instead of in the
# project's build script
mkdir emulator
cp tools/emulator emulator/

# Cleanup
rm -fR $android_release_dir $android_release_dir_old

# Archive dist directory
cd /var/tmp/dist
[% c('tar', {
        tar_src => [ project ],
        tar_args => '-czf ' _ dest_dir _ '/' _ c('filename'),
        }) %]
